/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.SampleItem;
import com.xuexiang.xuidemo.provider.SampleItemProvider;
import com.xuexiang.xuidemo.util.PagerSnapHelper;

import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * xinglu
 *
 * @since 2021-04-22
 */
public class SnapHelperAbilitySlice extends BaseAbilitySlice implements Component.ClickedListener {

    private static final int DIRECTION_UP = 1;
    private static final int DIRECTION_RIGHT = 4;
    private static final int DIRECTION_DOWN = 2;
    private static final int DIRECTION_LEFT = 3;
    private Image mImageBack;
    private Image mImageMenu;
    private ListContainer mListContainer;
    private List<SampleItem> list;

    // ListContainer滑动方向
    private int moveDirection = 0;

    // 当前被选中的item position
    private int currentPosition = 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_slice_snapehelper);
        initview();
        initListContainer();
        initClickedListener();
    }

    private void initListContainer() {
        list = getData();
        SampleItemProvider sampleItemProvider = new SampleItemProvider(list, this);
        mListContainer.setItemProvider(sampleItemProvider);
        PagerSnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(mListContainer, list);
    }


    private void initview() {
        mImageBack = (Image) findComponentById(ResourceTable.Id_image_back);
        mImageMenu = (Image) findComponentById(ResourceTable.Id_image_menu);
        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer);


    }

    private void initClickedListener() {
        mImageBack.setClickedListener(this);
        mImageMenu.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
            case ResourceTable.Id_image_menu:
                showDialog();
                break;

            default:
                break;
        }
    }

    private void showDialog() {

        CommonDialog commonDialog = new CommonDialog(getContext());
        Component component =
            LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_dialog_snaphelper_bottom_layout, null,
                false);
        Text mTextHorizontal = (Text) component.findComponentById(ResourceTable.Id_text_horizontal);
        Text mTextVertical = (Text) component.findComponentById(ResourceTable.Id_text_vertical);
        commonDialog.setTransparent(true);
        commonDialog.setContentCustomComponent(component);

        mTextHorizontal.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mListContainer.setOrientation(Component.HORIZONTAL);
                commonDialog.hide();
            }
        });

        mTextVertical.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mListContainer.setOrientation(Component.VERTICAL);
                commonDialog.hide();
            }
        });

        commonDialog.show();
    }


    private ArrayList<SampleItem> getData() {
        ArrayList<SampleItem> listData = new ArrayList<>();
        for (int i = 0; i <= 8; i++) {
            listData.add(new SampleItem("" + i));
        }
        return listData;
    }
}
